package com.lagou.bak;

import org.apache.directory.shared.kerberos.codec.apRep.actions.ApRepInit;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * 模拟观察从kafka获取数据程序中断
 */
public class LucasKafka {
    static int count = 0;
    public static void main(String[] args) throws Exception {
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        Configuration configuration = new Configuration();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);
//        env.setParallelism(1);
        env.enableCheckpointing(10);
        env.setStateBackend(new FsStateBackend("file:////tmp/lucasdb"));
        CheckpointConfig config = env.getCheckpointConfig();
        config.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        config.setCheckpointInterval(20);

        String topic = "number2";
        SimpleStringSchema simpleStringSchema = new SimpleStringSchema();
        Properties props = new Properties();
        props.setProperty("bootstrap.servers","hdp-2:9092");
        props.put("partitioner.class", "org.apache.kafka.clients.producer.RoundRobinPartitioner");
        props.setProperty("group.id","mygp");
        FlinkKafkaConsumer<String> consumer = new FlinkKafkaConsumer<>(topic, simpleStringSchema, props);
        consumer.setStartFromEarliest();
        DataStreamSource<String> data = env.addSource(consumer);
//        data.print();
        SingleOutputStreamOperator<String> maped = data.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                count++;
                if (count == 10000) {
                    count = 0;
                    throw new Exception("error.");
                }
                return value;
            }
        });
        maped.print();
        env.execute();
    }
}
